package com.hellobike.haxing.modules.operate.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hellobike.haxing.common.annotation.AutoLog;
import com.hellobike.haxing.common.system.base.controller.BaseController;
import com.hellobike.haxing.common.system.query.QueryGenerator;
import com.hellobike.haxing.common.vo.Result;
import com.hellobike.haxing.modules.operate.entity.PlaybillImg;
import com.hellobike.haxing.modules.operate.mapper.PlaybillImgMapper;
import com.hellobike.haxing.modules.operate.service.IPlaybillImgService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.jeecgframework.core.util.ApplicationContextUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.stream.Collectors;

/**
 * @Description: 海报分享配置
 * @Author: haxing-boot
 * @Date: 2020-11-17
 * @Version: V1.0
 */
@RestController
@RequestMapping("/operate/playbillImg")
@Slf4j
@Data
public class PlaybillImgController extends BaseController<PlaybillImg, IPlaybillImgService> {

    @Autowired
    private IPlaybillImgService playbillImgService;

    @Autowired
    private PlaybillImgMapper playbillImgMapper;




    @PostMapping(value = "/info")
    public Result<PlaybillImg> info() {
        Result<PlaybillImg> result = new Result<>();
        PlaybillImg playbillImg = playbillImgService.getBaseMapper().selectOne(new QueryWrapper<PlaybillImg>()
                .lambda().eq(PlaybillImg::getDeleted, 0).orderByDesc(PlaybillImg::getId).last("limit 1"));
        result.success("").setResult(playbillImg);
        return result;
    }

    /**
     * 分页列表查询
     *
     * @param playbillImg
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "海报分享配置-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(PlaybillImg playbillImg,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<PlaybillImg> queryWrapper = QueryGenerator.initQueryWrapper(playbillImg, req.getParameterMap());
        Page<PlaybillImg> page = new Page<PlaybillImg>(pageNo, pageSize);
        IPage<PlaybillImg> pageList = playbillImgService.page(page, queryWrapper);
        return Result.ok(pageList);
    }

    /**
     * 添加
     *
     * @param playbillImg
     * @return
     */
    @AutoLog(value = "海报分享配置-添加", operateType = 2)
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody PlaybillImg playbillImg) {
        playbillImgService.save(playbillImg);
        return Result.ok("添加成功！");
    }

    /**
     * 编辑
     *
     * @param playbillImg
     * @return
     */
    @AutoLog(value = "海报分享配置-编辑", operateType = 3)
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody PlaybillImg playbillImg) {
        PlaybillImg result = playbillImgService.getBaseMapper().selectOne(new QueryWrapper<PlaybillImg>()
                .lambda().eq(PlaybillImg::getDeleted, 0).orderByDesc(PlaybillImg::getId).last("limit 1"));
        if(result == null) {
            this.playbillImgService.save(playbillImg);
        }else {
            result.setCampaignPlaybillUrl(playbillImg.getCampaignPlaybillUrl());
            result.setShopPlaybillUrl(playbillImg.getShopPlaybillUrl());
            this.playbillImgService.updateById(result);
        }
        return Result.ok("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "海报分享配置-删除", operateType = 4)
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) Long id) {
        playbillImgService.removeById(id);
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "海报分享配置-批量删除", operateType = 4)
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.playbillImgService.removeByIds(Arrays.asList(ids.split(",")).stream().map(s -> Long.parseLong(s.trim())).collect(Collectors.toList()));
        return Result.ok("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) Long id) {
        PlaybillImg playbillImg = playbillImgService.getById(id);
        if (playbillImg == null) {
            return Result.error("未找到对应数据");
        }
        return Result.ok(playbillImg);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param playbillImg
     */
    @AutoLog(value = "海报分享配置-导出excel", operateType = 6)
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, PlaybillImg playbillImg) {
        return super.exportXls(request, playbillImg, PlaybillImg.class, "海报分享配置");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @AutoLog(value = "海报分享配置-导入excel", operateType = 5)
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, PlaybillImg.class);
    }

}
